package com.gang.music.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gang.music.common.RegisterStatistics;
import com.gang.music.core.shiro.ShiroKit;
import com.gang.music.system.entity.User;
import com.gang.music.system.mapper.UserMapper;
import com.gang.music.system.service.UserService;
import com.smallchill.core.toolbox.kit.DateTimeKit;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gang
 * @since 2017-10-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

	public static final String MD5 = "MD5";
	public static final int HASH_INTERATIONS = 1024;
	
	@Autowired
	private UserMapper userMapper;
	
	@Override
	public Boolean ckeckLoginName(String loginName) {
		
		
		return userMapper.selectByLoginName(loginName) == null;
	}
	
	@Override
	public void registerUser(User user) {
		
		entryptPassword(user);
		user.setRegisterDate(DateTimeKit.date());
		userMapper.insert(user);
	}
	
	/**
	 * 设定安全的密码，生成随机的salt并经过1024次 sha-1 hash
	 */
	private void entryptPassword(User user) {

		String hashAlgorithmName = MD5;
		Object credentials = user.getPlainPassword();
		//获得随机盐
		String saltStr = ShiroKit.getRandomSalt(5);
		user.setSalt(saltStr);
		
		Object salt = ByteSource.Util.bytes(saltStr);
		int hashIterations = HASH_INTERATIONS;
		Object result = new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
		user.setPassword(result.toString());
	}

	@Override
	public List<User> findUserByTodayRegister() {
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		
		Date beginDate = calendar.getTime();
		Date currentDate = new Date();
		
		EntityWrapper<User> ew = new EntityWrapper<>();
		ew.between("register_date", beginDate, currentDate);
		return userMapper.selectList(ew);
	}

	@Override
	public List<User> findAllWithSongList() {
		
		return userMapper.selectWithSong();
	}

	@Override
	public Long queryTotalUserNum() {
		
		return userMapper.queryTotalUserNum();
	}

	@Override
	public List<Integer> findMenuIdsByUserId(Long userId) {
		
		return userMapper.selectMenuIdsByUserId(userId);
	}

	@Override
	public List<String> findUserRoleCodes(Long id) {
		
		return userMapper.selectUserRoleCodes(id);
	}

	@Override
	public List<Integer> findRoleIdsByUserId(Long userId) {
		
		return userMapper.selectRoleIdsByUserId(userId);
	}

	@Override
	public List<String> findPermssionsByUserId(Long userId) {
		
		return userMapper.selectPermssionsByUserId(userId);
	}

	@Override
	public List<RegisterStatistics> findRegisterStatistics(Date brefDate,
			Date currDate) {
		
		return userMapper.selectRegisterStatisticsMap(brefDate, currDate);
	}

}
